//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.IO;

namespace Microsoft.TridentWordAddIn.Common
{
    public sealed class DirectoryHelper
    {
        private const string TempDirectoryName = @"Temp\TridentAddInTemp";
        private const string Folder = @"\TridentWordAddIn";

        private DirectoryHelper() { }

        /// <summary>
        /// Creates the temporary directory.
        /// </summary>
        /// <returns>string</returns>
        public static string CreateTemporaryDirectory()
        {
            string localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string tempDirectoryPath = Path.Combine(localFolder, TempDirectoryName);

            try
            {
                DirectoryHelper.CreateDirectory(localFolder);
            }
            catch (IOException)
            {
                return localFolder;
            }

            return tempDirectoryPath;
        }

        /// <summary>
        /// Creates the directory.
        /// </summary>
        /// <param name="path">The path.</param>
        public static void CreateDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// Clears the temporary directory.
        /// </summary>
        public static void ClearTemporaryDirectory()
        {
            string localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string tempDirectoryPath = Path.Combine(localFolder, TempDirectoryName);

            if (Directory.Exists(tempDirectoryPath))
            {
                string[] temporaryfiles = Directory.GetFiles(tempDirectoryPath);
                if (temporaryfiles != null)
                {
                    foreach (string file in temporaryfiles)
                    {
                        try
                        {
                            File.Delete(file);
                        }
                        catch (IOException) { }
                    }
                }
            }
        }

        /// <summary>
        /// Gets the user trident directory.
        /// </summary>
        /// <returns></returns>
        public static string UserTridentDirectory
        {
            get
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                return path + Folder;
            }
        }
    }
}
